package cn.signalpha.common.config;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.secure.TokenUser;
import cn.signalpha.core.swagger.EnableSwagger;
import cn.signalpha.core.swagger.SwaggerProperties;
import cn.signalpha.core.swagger.SwaggerUtil;
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.Collections;
import java.util.List;

/**
 * Swagger配置类
 */
@EnableSwagger
@AllArgsConstructor
@Configuration(proxyBeanMethods = false)
public class SwaggerConfiguration {

	/**
	 * 引入swagger配置类
	 */
	private final SwaggerProperties swaggerProperties;

	/**
	 * 引入Knife4j扩展类
	 */
	private final OpenApiExtensionResolver openApiExtensionResolver;

	@Bean
	public Docket authDocket() {
		return docket("授权模块", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.auth"));
	}

	@Bean
	public Docket baseApiDocket() {
		return docket("基础功能（前端）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.base.controller.api"));
	}
	@Bean
	public Docket baseAdminDocket() {
		return docket("基础功能（后台）", Lists.newArrayList(AppConstant.BASE_PACKAGES + ".modules.base.controller.admin", AppConstant.BASE_PACKAGES + ".modules.base.controller.wx"));
	}

	@Bean
	public Docket comApiDocket() {
		return docket("业务模块（前端）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.com.controller.api"));
	}
	@Bean
	public Docket comAdminDocket() {
		return docket("业务模块（后台）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.com.controller.admin"));
	}
	@Bean
	public Docket comSellerDocket() {
		return docket("业务模块（商户）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.com.controller.seller"));
	}

	@Bean
	public Docket shopApiDocket() {
		return docket("商城模块（前端）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.shop.controller.api"));
	}
	@Bean
	public Docket shopAdminDocket() {
		return docket("商城模块（后台）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.shop.controller.admin"));
	}
	@Bean
	public Docket shopSellerDocket() {
		return docket("商城模块（商户）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.shop.controller.seller"));
	}

	@Bean
	public Docket socialApiDocket() {
		return docket("社区模块（前端）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.social.controller.api"));
	}
	@Bean
	public Docket socialAdminDocket() {
		return docket("社区模块（后台）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.social.controller.admin"));
	}

	@Bean
	public Docket payDocket() {
		return docket("支付模块", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.pay.controller"));
	}

	@Bean
	public Docket scheduleDocket() {
		return docket("定时任务（后台）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.schedule.controller"));
	}
	@Bean
	public Docket resourceDocket() {
		return docket("资源管理（后台）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.resource.controller"));
	}
	@Bean
	public Docket resourceEndpointDocket() {
		return docket("资源管理（通用）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.resource.endpoint"));
	}
	@Bean
	public Docket systemDocket() {
		return docket("后台系统（后台）", Collections.singletonList(AppConstant.BASE_PACKAGES + ".modules.system.controller"));
	}

	private Docket docket(String groupName, List<String> basePackages) {
		return new Docket(DocumentationType.SWAGGER_2)
			.groupName(groupName)
			.apiInfo(apiInfo())
			.ignoredParameterTypes(TokenUser.class)
			.select()
			.apis(SwaggerUtil.basePackages(basePackages))
			.paths(PathSelectors.any())
			.build().securityContexts(securityContexts()).securitySchemes(securitySchemas())
			.extensions(openApiExtensionResolver.buildExtensions(groupName));
	}

	private List<SecurityContext> securityContexts() {
		return Collections.singletonList(SecurityContext.builder()
			.securityReferences(defaultAuth())
			.forPaths(PathSelectors.regex("^.*$"))
			.build());
	}

	List<SecurityReference> defaultAuth() {
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverywhere");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		return Lists.newArrayList(new SecurityReference(SwaggerUtil.client().getName(), authorizationScopes),
			new SecurityReference(SwaggerUtil.token().getName(), authorizationScopes));
	}

	private List<SecurityScheme> securitySchemas() {
		return Lists.newArrayList(SwaggerUtil.client(), SwaggerUtil.token());
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
			.title(swaggerProperties.getTitle())
			.description(swaggerProperties.getDescription())
			.license(swaggerProperties.getLicense())
			.licenseUrl(swaggerProperties.getLicenseUrl())
			.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
			.contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
			.version(swaggerProperties.getVersion())
			.build();
	}

}
